<?php

class Application_Model_Fotos extends Zend_Db_Table_Abstract
{
    protected $_name = 'fotos';
    protected $_primary = 'fotoId';
    protected $_rowClass = 'Application_Model_Foto';
    protected $_referenceMap = array(
        'Eventos' => array(
            'columns' => array('eventos_evId'),
            'refTableClass' => 'Application_Model_Eventos',
            'refColumns' => array('evId')
        ),
    );
    
    /**
     * @param int $id
     * @return Application_Model_Foto
     */
    public function getFotoById($id)
    {
        return $this->find($id)->current();
    }
    
    /** 
     * Fetch a dependent fotos rowset by evenos row 
     *
     * @param Application_Model_Evento $evRow
     * @return Zend_Db_Table_Rowset 
     */
    public function getFotosByEvento(Application_Model_Evento $evRow)
    {
        return $evRow->findDependentRowset('Application_Model_Fotos');
    }
    
    public function createFoto($isDefault, $fotoName, $fotoTitle, $evId)
    {
        $fotoRow = $this->createRow();
        if ($fotoRow) {
            $fotoRow->isDefault = $isDefault;
            $fotoRow->fotoPath = '/img/eventi/' . $fotoName;
            $fotoRow->fotoTitle = $fotoTitle;
            $fotoRow->eventos_evId = $evId;
            $fotoRow->save();
        } else {
            throw new Zend_Exception("Impossibile inserire le foto nel database");
        }
    }
    
    public function deleteFotosByEvento(Application_Model_Evento $evRow)
    {
        $rowsetFoto = $this->getFotosByEvento($evRow);
        foreach ($rowsetFoto as $row) {
            $row->delete();
        }
    }
}

